local Menu = GameState:addState('Menu')

function Menu:enterState()

	print("MENU entering state")

	--GS:gotoState('Gameplay')
	Game.state = "menu"
	
	if not menu_data then

		menu_data = {}
		menu_data.gui = {}
		menu_data.gui_id = {}
		menu_data.x = {}
		menu_data.x2 = {}
		menu_data.y = {}
		menu_data.y2 = {}
		menu_data.hgh = {}
		menu_data.wid = {}
	

		self:draw_menu()
	end

	if Game.initial_state then -- first loaded
		print("MENU initial state")
		

	else -- other visits to menu
		
		local top_layer = #MOAIRenderMgr.getRenderTable()+1
		Game:render_layer(gui:layer(), nil, top_layer)
		local layer = gui:layer()
		layer:setVisible(true)
		
		for i = 1, #menu_data.gui do
			
			Menu:show_gui(i)
			if menu_data.gui[i]:getText() == "New Game" then
			
				menu_data.gui[i]:setText("Continue")
			
			end
		
		end

	end



end

function Menu:draw_menu()

	self:draw_menu_item("New Game")
	self:draw_menu_item("Quit")



end


function Menu:draw_menu_item(text)

	function menuClick()
		--require('mobdebug').on()


		print("in menuClick")

		local text = self:getText()

		print(text)

	end
	local iter = #menu_data.gui+1
	local posX = 50 - 5
	local posY
	local wid = 10
	local hgh = 5

	if iter == 1 then
		posY = 50-2.5
		menu_data.gui[iter], menu_data.gui_id[iter] =
			GuiManager:new_popup("window", 
			posX, posY, wid, hgh, text)
	else
		local y_offset = (5/2)*iter
		posY = 50+y_offset-2.5+0.5
		menu_data.gui[iter], menu_data.gui_id[iter] =
			GuiManager:new_popup("window", 
			posX, posY, wid, hgh, text)

	end
	menu_data.gui[iter]:setBackgroundImage("assets\\8metal.png")
	menu_data.gui[iter]:setTextAlignment(menu_data.gui[iter].TEXT_ALIGN_CENTER,
		menu_data.gui[iter].TEXT_ALIGN_CENTER)

	--menu_data.gui[iter]:registerEventHandler(menu_data.gui[iter].EVENT_MOUSE_CLICK, nil, menuClick)
	-- can't get above event handler to work so going to manually detect mouse clicks
	
	local x = (SCREEN_RESOLUTION_X/100)*posX
	local y = (SCREEN_RESOLUTION_Y/100)*posY
	
	wid = wid*GUI_CONVERT_X
	hgh = hgh*GUI_CONVERT_Y
	--x, y = menu_data.gui[iter]:getFullPos()
	
	print("MENU GUI SCREEN POS " .. iter .. 
		" x: " .. x .. " y: " .. y)
	
	menu_data.x[iter] = x
	menu_data.y[iter] = y
	menu_data.x2[iter] = x+wid
	menu_data.y2[iter] = y+hgh
	menu_data.hgh[iter] = hgh
	menu_data.wid[iter] = wid
	
	
	menu_data.set = true


end



function Menu:hide_gui(id)

	menu_data.gui[id]:hide()

end

function Menu:show_gui(id)

	menu_data.gui[id]:show()

end


function Menu:exitState()

	print("MENU exiting state")
	for i = 1, #menu_data.gui do
	
		Menu:hide_gui(i)
	
	end
	


end
